const path = require('path');
const os = require('os');
const TerserPlugin = require('terser-webpack-plugin');

const packageJsonInfo = require(path.join(process.cwd(), 'package.json'));

const vendorVersion = packageJsonInfo.commonDependencies.vendor;

console.log('process.env.NODE_ENV', process.env.NODE_ENV);

const isProduction = process.env.NODE_ENV === 'production';

const config = {
  target: ['web', 'es5'],
  // devtool: 'eval-cheap-module-source-map',
  mode: process.env.NODE_ENV,
  entry: path.join(process.cwd(), 'scripts/vendor-externals.js'),
  output: {
    path: path.join(process.cwd(), 'vendors'),
    filename: !isProduction ? 'vendor.dev.js' : `vendor.${vendorVersion}.js`,
    libraryTarget: 'window',
  },
};

if (isProduction) {
  config.devtool = false;
  config.optimization = {
    minimizer: [
      new TerserPlugin({
        // 不提取注释 到单独的文件中
        extractComments: false,
        test: /\.js(\?.*)?$/i,
        parallel: os.cpus().length - 1,
        // minify: TerserPlugin.esbuildMinify,
        terserOptions: {
          // ecma: 5,
        },
      }),
    ],
  };
}

module.exports = config;
